﻿@using System.Text
@{
    ViewData["Title"] = "触发器";
    var plist = ViewBag.plist as StaticPagedList<QRTZ_TRIGGERS>;
    var groups = ViewBag.groups as IReadOnlyCollection<string>;
}

@section Styles
{
    <style>
        .tag{
            color: #666;
            padding: 2px;
            background-color: #eee;
        }
    </style>
}

<partial name="breadcrumb"/>

<div class="layui-fluid">

    <div class="layui-card">

        <div class="layui-card-body">
            <form class="layui-form layui-clear" lay-filter="searchFrm">
                <div class="layui-inline">
                    <input class="layui-input ml-0" name="name" placeholder="名称" id="name" value="@Context.Request.Query["name"]"/>
                </div>
                <div class="layui-inline">
                    @* <input class="layui-input ml-0" name="group" placeholder="分组" id="group" value="@Context.Request.Query["group"]"/> *@
                    <select class="layui-select" name="group" id="group">
                        <option value="">-全部分组-</option>
                        @foreach (var group in groups)
                        {
                            <option value="@group">@group</option>
                        }
                    </select>
                </div>
                <div class="layui-inline">
                    <select class="layui-select" name="state" id="state">
                        <option value="">-全部状态-</option>
                        <option value="ACQUIRED">ACQUIRED</option>
                        <option value="PAUSED">PAUSED</option>
                        <option value="WAITING">WAITING</option>
                        <option value="COMPLETE">COMPLETE</option>
                    </select>

                </div>

                <div class="layui-inline">
                    <select class="layui-select" name="type" id="type">
                        <option value="">-全部类型-</option>
                        <option value="SIMPLE">SIMPLE</option>
                        <option value="CRON">CRON</option>
                        <option value="CAL_INT">CAL_INT</option>
                        <option value="DAILY_I">DAILY_I</option>

                    </select>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn" lay-submit="" lay-filter="sreach">
                        <i class="layui-icon">&#xe615;</i>
                    </button>
                </div>

                <div class="layui-inline" style="float: right">
                    <button type="button" class="layui-btn layui-btn-normal" onclick="xadmin.open('添加任务', '@Url.Action("Edit", new {parentId = 0})', 0,0,true)">
                        <i class="layui-icon layui-icon-add-circle-fine"></i>添加任务
                    </button>
                </div>

            </form>
        </div>


        <div class="layui-card-body">

            <table class="layui-table">
                <thead>
                <tr>
                    <th>基本信息</th>
                    <th>类型</th>
                    <th>上下触发时间</th>
                    <th>开始结束时间</th>
                    <th style="width: 30%">关联数据</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @foreach (var item in plist)
                {
                    <tr>
                        <td>
                            @(item.TRIGGER_GROUP + "." + item.TRIGGER_NAME)<br/>
                            <small class="tag">@item.TRIGGER_STATE</small><br/>
                            @item.DESCRIPTION
                        </td>
                        <td>
                            <a href="javascript:;" onclick="xadmin.open('触发器详细', '@Url.Action("Detail", new {name = item.TRIGGER_NAME, group = item.TRIGGER_GROUP, type = item.TRIGGER_TYPE})', 400,250)">@item.TRIGGER_TYPE</a><br/>
                            @item.GetMisFireINSTR()
                        </td>
                        <td>
                            ↑: @item.GetTime(item.PREV_FIRE_TIME) <br/>
                            ↓: @item.GetTime(item.NEXT_FIRE_TIME)
                        </td>
                        <td>
                            ►: @item.GetTime(item.START_TIME)<br/>
                            ◉: @item.GetTime(item.END_TIME)
                        </td>
                        <td> @Encoding.UTF8.GetString(item.JOB_DATA) </td>
                        <td class="td-manage">
                            <div class="layui-btn-group">

                                @if (item.TRIGGER_STATE == "PAUSED")
                                {
                                    <button title="恢复" type="button" class="layui-btn layui-btn-warm" onclick="ResumeTrigger('@item.TRIGGER_NAME','@item.TRIGGER_GROUP')">
                                        <i class="layui-icon layui-icon-play"></i>
                                    </button>
                                }
                                else
                                {
                                    <button title="暂停" type="button" class="layui-btn  layui-btn-normal" onclick="PauseTrigger('@item.TRIGGER_NAME','@item.TRIGGER_GROUP')">
                                        <i class="layui-icon layui-icon-pause"></i>
                                    </button>
                                }
                                <button title="触发" type="button" class="layui-btn layui-btn-normal" onclick="TriggerJob('@item.TRIGGER_NAME','@item.TRIGGER_GROUP')">
                                    <i class="layui-icon layui-icon-fire"></i>
                                </button>

                                <button title="编辑" type="button" class="layui-btn layui-btn-normal" onclick="xadmin.open('编辑任务', '@Url.Action("Edit", new {name = item.TRIGGER_NAME, group = item.TRIGGER_GROUP, type = item.TRIGGER_TYPE})', 0,0,true)">
                                    <i class="layui-icon layui-icon-edit"></i>
                                </button>

                                <button title="删除" type="button" class="layui-btn layui-btn-danger" onclick="UnscheduleJob('@item.TRIGGER_NAME','@item.TRIGGER_GROUP')">
                                    <i class="layui-icon layui-icon-delete"></i>
                                </button>

                            </div>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
        <div class="layui-card-body">
            <div class="page">
                <my-pager list="plist"></my-pager>
            </div>
        </div>
    </div>

</div>


@section Scripts{
    <script>
    
    $(function(){
        
       $("#group").val('@Context.Request.Query["group"]');
       $("#state").val('@Context.Request.Query["state"]');
       $("#type").val('@Context.Request.Query["type"]');
       layui.form.render('select', 'searchFrm');
    });
    
    function UnscheduleJob(name,group){
        layer.confirm('确定要删除触发器吗？',
                        function() {
                            $.post("@Url.Action("UnscheduleJob")",
                                { name: name,group:group },
                                function(ro) {
                                    layer.msg(ro.msg,
                                        { time: 500 },
                                        function() {
                                            if (ro.success) {
                                                location.reload();
                                            }
                                        });
                                });
                        });
                }
                
    function ResumeTrigger(name,group){
         $.post("@Url.Action("ResumeTrigger")",
                                         { name: name,group:group },
         function(ro) {
             location.reload();
         });
    }
           
    function PauseTrigger(name,group){
        $.post("@Url.Action("PauseTrigger")",
        { name: name,group:group },
        function(ro) {
            location.reload();
        });
    }
    
    function TriggerJob(name,group){
        
        layer.confirm('确定立刻触发吗？',function() {
                    $.post("@Url.Action("TriggerJob")",
                            { name: name,group:group },
                            function(ro) {
                                layer.msg("触发成功",{ time: 500 });
                            });               
                });
    }   
    </script>
}